package com.qfedu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.BoxEntity;
import com.qfedu.kit.FruitsFinal;
import com.qfedu.service.BoxService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class boxController {

    @Reference(check = false)
    BoxService boxService;
    //跳转集装箱界面
    @RequestMapping("jumpbox")
    public String jumpbox(BoxEntity be,HttpServletRequest request){

        PageInfo<BoxEntity> fenyebox = boxService.fenyebox(be);
        //List<BoxEntity> queryall = boxService.queryall();
        request.setAttribute("be",be);
        request.setAttribute("pageInfo",fenyebox);
        return "boxlist";
    }
    @RequestMapping("qiyong")
    @ResponseBody
    public String qiyong(int bid){

        BoxEntity be = boxService.queryBox(bid);
        int bzt = be.getBzt();
        if (bzt==0){
            bzt=1;
            boolean ting = boxService.qiyong(bid, bzt);
            return FruitsFinal.FILED_RESULT;//0001将其停用
        }
        if (bzt==1){
            bzt = 0;
            boolean qi = boxService.qiyong(bid,bzt);
            return FruitsFinal.SUCCESS_RESULT;//0000启用
        }

        return "";

    }

    //真删除
    @RequestMapping("delbox")
    @ResponseBody
    public String delbox(int bid){
        boolean delbox = boxService.delbox(bid);
        if (delbox){

            return FruitsFinal.SUCCESS_RESULT;//0000
        }
        return FruitsFinal.FILED_RESULT;//0001
    }

    //跳转添加页面
    @RequestMapping("jumpaddbox")
    public String jumpaddbox(){
        return "addbox";
    }

    //添加集装箱
    @RequestMapping("addbox")
    @ResponseBody
    public String addbox(BoxEntity be){
        boolean addbox = boxService.addbox(be);
        if (addbox){
            return FruitsFinal.SUCCESS_RESULT;//0000成功
        }

        return "0001";
    }

    //存储修改时回填的数据
    @RequestMapping("shuju")
    @ResponseBody
    public String shuju(int bid,HttpServletRequest request){
        BoxEntity boxEntity = boxService.queryBox(bid);
        request.getSession().setAttribute("box",boxEntity);
        return FruitsFinal.SUCCESS_RESULT;//0000
    }
    //跳转修改
    @RequestMapping("jumpupdbox")
    public String jumpupdbox(){

//        BoxEntity boxEntity = boxService.queryBox(bid);
//        request.setAttribute("box",boxEntity);

        return "updbox";
    }

    //修改
    @RequestMapping("updbox")
    @ResponseBody
    public String updbox(BoxEntity be,HttpServletRequest request){
        boolean updbox = boxService.updbox(be);
        if (updbox){
            request.getSession().removeAttribute("box");//清除修改时回填的老数据
            return FruitsFinal.SUCCESS_RESULT;//0000成功
        }

        return "0001";
    }
    //选择删除
    @RequestMapping("delByIds")
    @ResponseBody
    public String delByIds(String bids){
        if (bids==""){
            return FruitsFinal.FILED_RESULT;//0001
        }
        boolean b = boxService.delByIds(bids);
        if (b){
            return FruitsFinal.SUCCESS_RESULT;//0000
        }

        return "";
    }

}
